package com.hyphenate.easeui.widget.chatrow;

import com.bumptech.glide.Glide;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMMessage.ChatType;
import com.hyphenate.chat.EMTextMessageBody;
import com.hyphenate.easeui.R;
import com.hyphenate.easeui.utils.EaseSmileUtils;
import com.hyphenate.easeui.widget.EaseImageView;
import com.hyphenate.exceptions.HyphenateException;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.TextUtils;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TextView.BufferType;

import org.json.JSONObject;

public class EaseChatRowText extends EaseChatRow{

	private TextView contentView;
    private TextView autherNameView;
    private TextView autherTagView,setType,messageTypeTag,muteMessageView;
    private EaseImageView userAvatar;
    private RelativeLayout allMessage;
    private RelativeLayout typeContain;

    public EaseChatRowText(Context context, EMMessage message, int position, BaseAdapter adapter) {
		super(context, message, position, adapter);
	}

	@Override
	protected void onInflateView() {
		inflater.inflate(message.direct() == EMMessage.Direct.RECEIVE ?
				R.layout.ease_row_received_message : R.layout.ease_row_sent_message, this);
	}

	@Override
	protected void onFindViewById() {
		contentView = (TextView) findViewById(R.id.tv_chatcontent);
        autherNameView = (TextView) findViewById(R.id.tv_auther_name);
        autherTagView = (TextView) findViewById(R.id.tv_auther_tag);
        userAvatar = (EaseImageView) findViewById(R.id.iv_userhead);
        typeContain =  (RelativeLayout)findViewById(R.id.tv_self_type_contain);
        allMessage  =   (RelativeLayout)findViewById(R.id.message_all_contain);
        setType = (TextView) findViewById(R.id.tv_self_type);
        messageTypeTag = (TextView) findViewById(R.id.tv_message_type_bag);
        muteMessageView = (TextView) findViewById(R.id.tv_mute_message_view);
	}






    @Override
    public void onSetUpView() {

        try {
            String type =  message.getStringAttribute("type");
            JSONObject authorObj =  message.getJSONObjectAttribute("author");
            if ("text".equals(type)){
                allMessage.setVisibility(VISIBLE);
                typeContain.setVisibility(GONE);
                messageTypeTag.setVisibility(GONE);
                muteMessageView.setVisibility(GONE);
            } else if ("mute".equals(type)){ //ok
                muteMessageView.setText("当前仅导师发言");
                muteMessageView.setVisibility(VISIBLE);
                typeContain.setVisibility(GONE);
                allMessage.setVisibility(GONE);
            }else if ("unmute".equals(type)){ //ok
                muteMessageView.setText("大家可自由发言");
                typeContain.setVisibility(GONE);
                allMessage.setVisibility(GONE);
                muteMessageView.setVisibility(VISIBLE);
            }else if ("target".equals(type)){ //ok
                setType.setText("明确目标");
                typeContain.setVisibility(VISIBLE);
                allMessage.setVisibility(VISIBLE);
                messageTypeTag.setVisibility(VISIBLE);
                messageTypeTag.setText("本期学习目标");
                muteMessageView.setVisibility(GONE);
            }else if ("discuss".equals(type)){//ok
                setType.setText("分享探讨");
                typeContain.setVisibility(VISIBLE);
                allMessage.setVisibility(GONE);
                messageTypeTag.setVisibility(GONE);
                muteMessageView.setVisibility(GONE);
            }else if ("question".equals(type)){ // ok
                typeContain.setVisibility(GONE);
                allMessage.setVisibility(VISIBLE);
                messageTypeTag.setVisibility(VISIBLE);
                muteMessageView.setVisibility(GONE);
                messageTypeTag.setText("问题");
            } else {
                allMessage.setVisibility(VISIBLE);
                typeContain.setVisibility(GONE);
                muteMessageView.setVisibility(GONE);
                messageTypeTag.setVisibility(GONE);
            }
            if (authorObj != null) {
                String authID = authorObj.optString("id", "");
                String authName = authorObj.optString("name", "");
                String roleid = authorObj.optString("role", "");
                String roleVale = authorRoleValue(roleid);
                String authAvatar = authorObj.optString("avatar", "");
                if (!TextUtils.isEmpty(authName)) {
                    autherNameView.setText(authName);
                }
                if (!TextUtils.isEmpty(authAvatar)) {
                    userAvatar.loadHeaderView(authAvatar);
                }
                if (!TextUtils.isEmpty(roleVale)) {
                    autherTagView.setText(roleVale);
                    autherTagView.setVisibility(VISIBLE);
                } else {
                    autherTagView.setVisibility(GONE);
                }
            }
        } catch (Exception e){

        }

        // 设置内容
        EMTextMessageBody txtBody = (EMTextMessageBody) message.getBody();
        Spannable span = EaseSmileUtils.getSmiledText(context, txtBody.getMessage());
        contentView.setText(span, BufferType.SPANNABLE);
        handleTextMessage();
    }

    protected void handleTextMessage() {
        if (message.direct() == EMMessage.Direct.SEND) {
            setMessageSendCallback();
            switch (message.status()) {
            case CREATE: 
                progressBar.setVisibility(View.GONE);
                statusView.setVisibility(View.VISIBLE);
                break;
            case SUCCESS:
                progressBar.setVisibility(View.GONE);
                statusView.setVisibility(View.GONE);
                break;
            case FAIL:
                progressBar.setVisibility(View.GONE);
                statusView.setVisibility(View.VISIBLE);
                break;
            case INPROGRESS:
                progressBar.setVisibility(View.VISIBLE);
                statusView.setVisibility(View.GONE);
                break;
            default:
               break;
            }
        }else{
            if(!message.isAcked() && message.getChatType() == ChatType.Chat){
                try {
                    EMClient.getInstance().chatManager().ackMessageRead(message.getFrom(), message.getMsgId());
                } catch (HyphenateException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    protected void onUpdateView() {
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onBubbleClick() {
        // TODO Auto-generated method stub
        
    }



}
